package com.xiang.design.strategy;


import com.xiang.design.strategy.Impl.SaveLogToFile;
import com.xiang.design.strategy.Impl.SaveLogToLocal;
import com.xiang.design.strategy.Impl.SaveLogToRemote;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 战略工厂(content上下文)
 *
 * @author xiangtianlei@gogpay.cn
 * @date 2022-09-28
 */
@Component
public class StrategyFactory {

    private static final Map<Integer, LogStrategy> strategyMap = new HashMap<>();
    /**
     * Map方法
     */
    private static final Map<LogEnum, LogStrategy> MAP = new HashMap<>();

    static {
        MAP.put(LogEnum.FILE, new SaveLogToFile());
        MAP.put(LogEnum.LOCAL, new SaveLogToLocal());
        MAP.put(LogEnum.REMOTE, new SaveLogToRemote());
    }

    /**
     * SpringBean方法
     */
    @Resource
    Set<LogStrategy> logStrategySet;

//-----------------------------------------------------------------------//

    @PostConstruct
    public void init() {
        System.out.println("logStrategySet" + logStrategySet);
        for (LogStrategy logStrategy : logStrategySet) {
            strategyMap.put(logStrategy.channel(), logStrategy);
        }
        System.out.println("strategyMap" + strategyMap);
    }

    /**
     * 通过管道数字获取日志实现类
     *
     * @param channel 通道
     * @see Integer
     * @see LogStrategy
     */
    public LogStrategy getLogByChannel(Integer channel) {
        return strategyMap.get(channel);
    }

    /**
     * 获取对应的实现类
     *
     * @param logEnum 名字
     * @see String
     * @see LogStrategy
     */
    public LogStrategy getLogByName(LogEnum logEnum) {
        return MAP.get(logEnum);
    }
}
